package SA.student;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class SAstudentActivity extends Activity {
	public String serveridpw1="";
	public String studentID="";
	public String studentPW="";

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		ImageView loginBtn = (ImageView) findViewById(R.id.login_btn);
		loginBtn.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				EditText eTextID = (EditText) findViewById(R.id.eTextID);
				EditText eTextPW = (EditText) findViewById(R.id.eTextPW);

				studentID = eTextID.getText().toString();
				studentPW = eTextPW.getText().toString();
				String clientidpw = studentID + studentPW;
				if(clientidpw.length()!=0)
					login(clientidpw);
				else{
					new AlertDialog.Builder(SAstudentActivity.this).setTitle("로그인 실패")
					.setMessage("아이디와 비밀번호를 다시 입력해주세요.").setCancelable(false)
					.setNegativeButton("확인", null).show();
				}
			}
		});
	}

	void login(String clientidpw) {

		Parse ps = new Parse();
		ps.run();//로그인 접속
		if (clientidpw.equals(serveridpw1)) {
			Intent intent = new Intent(this, MainActivity.class);
			intent.putExtra("ID",studentID);
			startActivity(intent);
		} 
		else {
			new AlertDialog.Builder(SAstudentActivity.this).setTitle("로그인 실패")
			.setMessage("올바른 아이디와 비밀번호를 다시 입력해주세요.").setCancelable(false)
			.setNegativeButton("확인", null).show();
		}
	}

	private class Parse implements Runnable {
		public void run() {
			// TODO Auto-generated method stub
			try {
				String addr = "Http://220.67.128.8/search.php?teacherID="+ studentID + "&teacherPW=" + studentPW;
				URL url = new URL(addr);
				HttpURLConnection conn = (HttpURLConnection) url.openConnection();

				InputStream is = null;
				ByteArrayOutputStream baos = null;
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					is = conn.getInputStream();
					baos = new ByteArrayOutputStream();
					byte[] byteBuffer = new byte[1024];
					byte[] byteData = null;
					int nLength = 0;
					while ((nLength = is.read(byteBuffer)) > 0) {
						baos.write(byteBuffer, 0, nLength);
					}
					byteData = baos.toByteArray();
					String response = new String(byteData);
					serveridpw1 = response;
					if (response == null || response.equals("")) {
						throw new Exception("Response is NULL!");
					}
				} else {
					Log.e("login 응답 : ", "접속 안됨");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
			}
		}
	}
	//////////////뒤로가기 버튼 종료////////////////////////////////////
	public boolean onKeyDown(int keyCode, KeyEvent event){
		switch(keyCode){
		
		case KeyEvent.KEYCODE_BACK:
			String alertTitle = getResources().getString(R.string.app_name);
			String buttonMessage = getResources().getString(R.string.alert_msg_exit);
			String buttonYes = getResources().getString(R.string.button_yes);
			String buttonNo = getResources().getString(R.string.button_no);

			new AlertDialog.Builder(SAstudentActivity.this)
			.setTitle(alertTitle)
			.setMessage(buttonMessage)
			.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					moveTaskToBack(true);
					finish();
				}
			})
			.setNegativeButton(buttonNo, null)
			.show();
		}
		return true;
	}
}